package cn.dujc.superkid.fragment.research;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.dujc.core.ui.BaseFragment;
import cn.dujc.superkid.R;
import cn.dujc.superkid.adapter.ContactUsAdpater;
import cn.dujc.superkid.api.HttpHelper;
import cn.dujc.superkid.api.QMap;
import cn.dujc.superkid.api.Result;
import cn.dujc.superkid.api.ResultCallBack;
import cn.dujc.superkid.bean.ContactResult;

/**
 * @author du
 * date 2018/8/2 上午12:11
 */


public class ContactUsFragment extends BaseFragment {
    private TextView tv_title,tv_telephone,tv_customerService,tv_address;
    private RecyclerView rv_contact;
    private ContactUsAdpater contactUsAdpater;

    private final List<ContactResult.ContactEntity> list = new ArrayList<>();
    @Override
    public int getViewId() {
        return R.layout.fragment_contactus;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        initview();
        loadData();
    }

    private void initview() {
        tv_title= (TextView) findViewById(R.id.tv_title);
        tv_telephone= (TextView) findViewById(R.id.tv_telephone);
        tv_customerService= (TextView) findViewById(R.id.tv_customerService);
        tv_address= (TextView) findViewById(R.id.tv_address);
        rv_contact= (RecyclerView) findViewById(R.id.rv_contact);
        rv_contact.setLayoutManager(new GridLayoutManager(getActivity(),2));
        contactUsAdpater = new ContactUsAdpater(list,getContext());
        rv_contact.setAdapter(contactUsAdpater);
    }

    private void loadData() {
        HttpHelper.getHome().contact(QMap.build().create()).enqueue(new ResultCallBack<Result<ContactResult>>(mActivity) {
            @Override
            public void onSuccessCallback(int code, Result<ContactResult> body) {
                list.clear();
                final ContactResult data = body.getData();
                tv_title.setText(data.getTitle());
                tv_telephone.setText(data.getTelephone());
                tv_customerService.setText(data.getCustomerService());
                tv_address.setText(data.getAddress());
                if (data != null) {
                    final List<ContactResult.ContactEntity> data1 = data.getContact();
                    list.addAll(data1);
                }
                contactUsAdpater.notifyDataSetChanged();
            }
        });
    }
}
